package com.ruanda.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanda.domain.ProdComm;
import com.ruanda.model.Result;
import com.ruanda.service.ProdCommService;
import com.ruanda.vo.ProdCommData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Tag(name = "评论接口管理")
@RequestMapping("prod/prodComm")
@RestController
public class ProdCommController {

    @Autowired
    private ProdCommService prodCommService;

    /**
     * 多条件分页查询评论列表
     * @param current 当前页
     * @param size 每页大小
     * @param prodName 商品名称
     * @param status 评论状态
     * @return
     */
    @Operation(summary = "多条件分页查询评论列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prodComm:page')")
    public Result<Page<ProdComm>> loadProdCommPage(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   @RequestParam(required = false) String prodName,
                                                   @RequestParam(required = false) Integer status) {
        Page<ProdComm> page = new Page<>(current, size);
        page = prodCommService.page(page, new LambdaQueryWrapper<ProdComm>()
                .eq(ObjectUtil.isNotNull(status), ProdComm::getStatus,status)
                .like(StringUtils.hasText(prodName), ProdComm::getProdName, prodName)
                .orderByDesc(ProdComm::getCreateTime)
        );
        return Result.success(page);
    }

    @Operation(summary = "根据标识查询评论详情")
    @GetMapping("{commId}")
    @PreAuthorize("hasAuthority('prod:prodComm:info')")
    public Result<ProdComm> loadProdCommInfo(@PathVariable Long commId) {
        ProdComm prodComm = prodCommService.getById(commId);
        return Result.success(prodComm);
    }

    @Operation(summary = "审核或回复商品评论")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prodComm:update')")
    public Result<String> modifyProdComm(@RequestBody ProdComm prodComm) {
        Integer count = prodCommService.examineOrReplyProdComm(prodComm);
        return Result.handle(count>0);
    }
    
    //////// 小程序 ////////
    
    @Operation(summary = "小程序查询商品评论总览信息")
    @GetMapping("prodComm/prodCommData")
    public Result<ProdCommData> loadWxProdCommData(@RequestParam Long prodId) {
        ProdCommData prodCommData = prodCommService.queryWxProdCommDataByProdId(prodId);
        return Result.success(prodCommData);
    }

    @Operation(summary = "小程序分页查询单个商品评论列表")
    @GetMapping("prodComm/prodCommPageByProd")
    public Result<Page<ProdComm>> loadWxProdCommPageByProd(@RequestParam Long current,
                                                           @RequestParam Long size,
                                                           @RequestParam Long prodId,
                                                           @RequestParam Long evaluate) {
        //根据商品分页查询商品评论
        Page<ProdComm> page = prodCommService.queryWxProdCommPageByProd(current, size, prodId, evaluate);
        return Result.success(page);
    }
    
}